/**
 * Created by xuan on 7/6/15.
 */
define(function (require,exports,module) {

    var util = require('util');

    var angular = require('angular');

    var material = angular.module('material');

    material.factory('picturesService',['http','uploaderService','apife','apibe',function(http,uploaderService,apife,apibe) {
        var LIMITE = 10;
        var obj;
        obj = {

            //propertys
            currGroupIndex : 0,

            groups : [],

            pictureList : [],

            pagination : {
                pages: 0,
                current: 1,
                change: function (page) {
                    obj.getList(page,obj.getCurrGroupId());
                }
            },
            newGroupObj : {
                title : "创建分组",
                text : function (index) {
                    return '';
                },
                validate: function (text) {
                    return text.length > 0;
                },
                ok: function (name) {
                    obj.addOrUpdateGroup('',name);
                }
            },

            renameGroupObj : {
                title : "编辑分组",
                text : function (index) {
                    return obj.groups[obj.currGroupIndex].name;
                },
                validate: function (text) {
                    return text.length > 0;
                },
                ok: function (name) {
                    obj.addOrUpdateGroup(obj.getCurrGroupId(),name);
                }
            },

            deleteGroupObj : {
                followType : 'text',
                title: "确定删除此分组吗？",
                text: function (index) {
                    return '';
                },
                validate: function (text) {
                    return true;
                },
                ok: function () {
                    obj.deleteGroup(obj.getCurrGroupId());
                }
            },

            editPictureObj : {
                title: "编辑名称",
                text: function (index) {
                    return obj.pictureList[index].name;
                },
                validate: function (text) {
                    return true;
                },
                ok: function (name,index) {
                    var p = obj.pictureList[index];
                    obj.addOrUpdatePicture(p.id,{data:{name:name},type:'picture'});
                }
            },

            movePictureObj : {
                followType : 'radio',
                title: '',

                //当前选中group id
                currId : '',
                text: function (index) {
                    return '';
                },
                validate: function (text) {
                    if(this.currId == ''){
                        util.notify('请选择分组');
                        return false;
                    }
                    return true;
                },
                getGroups : function(){
                    //过滤当前分组
                    return obj.groups.filter(function(o){return o.id != obj.getCurrGroupId()});
                },
                ok: function (name,index) {
                    var p = obj.pictureList[index];
                    obj.addOrUpdatePicture(p.id, {data: {group_id:this.currId},type:'picture'});
                },
                cancel : function (){
                    this.currId = '';
                }
            },

            deletePictureObj : {
                followType : 'text',
                title: "确定删除此图片吗？",
                text: function (index) {
                    return '';
                },
                validate: function (text) {
                    return true;
                },
                ok: function (name,id) {
                    obj.deletePicture(id);
                }
            },

            //methods
            getGroup : function(){
                obj.resetGroupId();

                http.get('material/picture-group-list')
                    .success(function(data, status, headers, config) {
                        if(data.retCode == 0){
                            obj.groups = data.data;
                        }
                    })
                    .error(function(data, status, headers, config) {

                    });

                obj.getList(1,'');
            },
            addOrUpdateGroup : function(id,name){
                http.post('material/save-picture-group',{id : id,name : name})
                    .success(function(data, status, headers, config) {
                        if(data.retCode == 0){
                            obj.getGroup();
                        }
                    })
                    .error(function(data, status, headers, config) {

                    });
            },
            deleteGroup : function(id){
                http.get('material/del-picture-group',{id : id})
                    .success(function(data, status, headers, config) {
                        if(data.retCode == 0){
                            obj.getGroup();
                        }
                    })
                    .error(function(data, status, headers, config) {

                    });
            },
            getCurrGroupId : function(){
                return obj.groups[obj.currGroupIndex].id;
            },
            resetGroupId : function(){
                return obj.currGroupIndex = 0;
            },
            getPictureById : function(id){
                for(var i = 0 ; i<obj.pictureList.length ; i++){
                    if(id === obj.pictureList[i].id)
                        return obj.pictureList[i]
                }
                return null;
            },
            getList : function(page,group_id){
                http.get('material/picture-list',{
                        limit : LIMITE,
                        offset : LIMITE * (page - 1),
                        group_id : group_id
                    })
                    .success(function(data, status, headers, config) {
                        if(data.retCode == 0){
                            obj.pictureList = data.data.data;
                            obj.pagination.pages = Math.ceil(data.data.total / LIMITE);
                        }
                    })
                    .error(function(data, status, headers, config) {

                    });
            },
            addOrUpdatePicture : function(id,data){
                http.post('material/save',data,{id : id})
                    .success(function(data, status, headers, config) {
                        if(data.retCode == 0){
                            obj.getGroup();
                        }
                    })
                    .error(function(data, status, headers, config) {

                    });
            },
            deletePicture : function(id){
                http.get('material/del-picture&',{id : id})
                    .success(function(data, status, headers, config) {
                        if(data.retCode == 0){
                            obj.getList(1,obj.getCurrGroupId());
                        }
                        if(data.retCode == -1){
                            console.error(errMsg);
                        }
                    })
                    .error(function(data, status, headers, config) {

                    });;
            },
            changeGroup : function(index){
                if(index !== obj.currGroupIndex){
                    obj.currGroupIndex = index;
                    obj.getList(1,obj.getCurrGroupId());
                }
            },
            getUpload : function(group_id){
                return uploaderService.createFileUploader({
                    api : 'material/upload',
                    filters : [
                        {
                            name: 'sizeLimit',
                            fn: function (item) {
                                return item.size <= 2 * 1024 * 1024;
                            }
                        }
                    ],
                    data : [{type : 'picture'}],
                    onBeforeUploadItem : function(fileItem){
                        fileItem.formData.push({
                            group_id : obj.getCurrGroupId()
                        });
                    },
                    success : function(){
                        obj.getList(1,obj.getCurrGroupId());
                        obj.groups[obj.currGroupIndex].total = parsetInt(obj.groups[obj.currGroupIndex].total) + 1;
                    },
                    fail : function(res){
                        res.msg && console.error(res.msg);
                    }
                })
            }
        };
        return obj;
    }]);
})